package bookshop.controller.front;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.CustComment;
import bookshop.domain.Customer;
import bookshop.service.BookService;
import bookshop.service.CustCommentService;
import bookshop.service.OrderService;
import bookshop.service.impl.BookServiceImpl;
import bookshop.service.impl.CustCommentServiceImpl;
import bookshop.service.impl.OrderServiceImpl;

public class CommentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act = request.getParameter("act");
		
		if("toComment".equals(act)){
			String bookCode = request.getParameter("bookCode");
			BookService bookService = new BookServiceImpl();
			
			request.setAttribute("book", bookService.getBookByCode(bookCode));
		    request.setAttribute("orderId", request.getParameter("orderId"));
			request.getRequestDispatcher("jsps/front/comment/input_comment.jsp").forward(request, response);
		}else if("input_comment".equals(act)){
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String bookCode = request.getParameter("bookCode");
			BookService bookService = new BookServiceImpl();
			String orderId = request.getParameter("orderId");
			OrderService orderService = new OrderServiceImpl();
			
			
			CustComment comment = new CustComment();
			comment.setOrderInfo(orderService.GetOrderByOrderId(orderId));
		    comment.setBook(bookService.getBookByCode(bookCode));
		    comment.setCustIp(request.getParameter("custIp"));
		    comment.setTitle(request.getParameter("title"));
		    comment.setContent(request.getParameter("content"));
		    comment.setCustomer(customer);
		    comment.setMark(Integer.parseInt(request.getParameter("mark")));
		    comment.setIssueTime(new java.util.Date());
		    
		    
		    
		    CustCommentService commentService =  new CustCommentServiceImpl();
		    commentService.saveCustComment(comment);
		    
		    
		    response.sendRedirect("myDD?act=CustOrder");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
